<template>
  <van-button
    class="follow-btn"
    :loading="followLoading"
    round
    size="small"
    v-if="isFollowed"
    @click="onFollow"
    >已关注</van-button
  >
  <van-button
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    v-else
    :loading="followLoading"
    @click="onFollow"
    >关注</van-button
  >
</template>

<script>
import { addFollow, deleteFollow } from "@/api/search.js";
export default {
  // model:{
  //   value:'isFollowed',
  //   event:'updateFollowed'
  // },
  props: {
    isFollowed: {
      type: Boolean,
      required: true,
    },
    autId: {
      type: [Number, String, Object],
      required: true,
    },
  },
  data() {
    return {
      followLoading: false,
    };
  },
  methods: {
    async onFollow() {
      this.followLoading = true; //展示关注按钮的loading状态
      try {
        if (this.isFollowed) {
          // 已关注,取消关注
          await deleteFollow(this.autId);
          this.$toast("取消成功");
        } else {
          // 未关注,添加关注
          await addFollow(this.autId);

          this.$toast("关注成功");
        }
        // 更新视图状态
        // this.detailObj.is_followed = !this.detailObj.is_followed;
        this.$emit("updateFollowed", !this.isFollowed);
      } catch (err) {
        let message = "数据加载失败";
        if (err.response && err.response.status == 400) {
          this.$toast("你不能关注你自己");
        }
        this.$toast(message);
      }
      this.followLoading = false;
    },
  },
};
</script>

<style>
</style>